package GUI;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MainGUI extends JFrame implements MouseListener, ActionListener, WindowListener {

    Maps map;
    JButton bStart = new JButton("New Game"); JButton bLoad = new JButton("Load");
    JButton bOptions = new JButton("Options"); JButton bBack = new JButton("Back");
    JButton bExit = new JButton("Exit"); JButton bOptSel = new JButton("Start");
    JPanel pTitle = new JPanel();
    JList listMaps = new JList();
    JLabel lVersion = new JLabel("v0.2");
    JMenuBar menu = new JMenuBar();

    public MainGUI() {
        setLayout(null);
        //init panels===========================================================
        pTitle.setBounds(200, 25, 400, 150);
        pTitle.setBackground(Color.blue);
        //init labels===========================================================
        lVersion.setSize(50, 10);
        lVersion.setLocation(735, 550);
        //init buttons==========================================================
        bStart.setSize(200, 50);
        bStart.setLocation(300, 200);
        bLoad.setSize(200, 50);
        bLoad.setLocation(300, 250);
        bLoad.setEnabled(false);
        bOptions.setSize(200, 50);
        bOptions.setLocation(300, 300);
        bBack.setSize(100, 50);
        bBack.setLocation(675, 500);
        bExit.setSize(200, 50);
        bExit.setLocation(300, 350);
        bOptSel.setSize(100, 50);
        bOptSel.setLocation(350, 50);
        //init list
        listMaps.setLocation(25, 50);
        listMaps.setSize(300, 300);
        listMaps.setSelectionMode(1);
        listMaps.setOpaque(false);
        //init menu=============================================================
        add(menu);

        //callbacks=============================================================
        addWindowListener(this);
        bStart.addActionListener(this);
        bOptSel.addActionListener(this);
        bLoad.addActionListener(this);
        bOptions.addActionListener(this);
        bBack.addActionListener(this);
        bExit.addActionListener(this);
        //set window============================================================
        setSize(800, 600);
        setLocationRelativeTo(null);
        setTitle("Tower Defense");
        setResizable(false);
        map = new Maps(this);
        mainMenu();
        setVisible(true);
    }

    private void mainMenu() {
        removeAll();
        add(bStart); add(bLoad); add(bOptions); add(bExit); add(pTitle); add(lVersion);
    }

    private void startMenu() {
        removeAll();
        listMaps.setListData(map.getMapList());
        add(bBack); add(listMaps); add(bOptSel);
    }

    private void optionsMenu() {
        removeAll();
        add(bBack);
    }

    //Overrides=================================================================    
    @Override
    public void actionPerformed(ActionEvent e) {
        String bPressed = e.getActionCommand();
        switch (bPressed) {
            case "New Game":
                startMenu();
                break;
            case "Load":
                System.out.println("Load Pressed");
                break;
            case "Options":
                optionsMenu();
                break;
            case "Back":
                mainMenu();
                break;
            case "Start":
                removeAll();
                map.createMap(listMaps.getSelectedIndex());
                add(map);
                break;
            case "Exit":
                System.exit(1);
        }
    }

    @Override
    public void removeAll() {
        remove(bStart); remove(bLoad); remove(bOptions); remove(bBack); remove(bExit);
        remove(pTitle); remove(lVersion); remove(listMaps); remove(bOptSel); remove(map);
        repaint();
    }

    @Override
    public void windowClosing(WindowEvent e) {
        System.exit(1);
    }

    @Override
    public void windowClosed(WindowEvent e) {
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void windowOpened(WindowEvent e) {
    }

    @Override
    public void windowIconified(WindowEvent e) {
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
    }

    @Override
    public void windowActivated(WindowEvent e) {
    }

    @Override
    public void windowDeactivated(WindowEvent e) {
    }
}